xarray.DataArray to xarray Dataset code example

Example: convert between dataarray and dataset using xarray

#import xarray
import xarray as xr

#open the dataset
ds = xr.open_dataset(file_name.nc)

#convert between dataarray and dataset
bands = ['var_1', 'var_2', 'var_3', ... , 'var_n']

    #da is a conversion to dataarray where we say we want a dataarray
    #containing only the variables inside bands and we want to convert
    #this subset of data to a dataarray
    #the second line is to convert back to dataset 
da = ds[bands].to_array(dim='enter a dimension of the dataset')
da.to_dataset(dim='enter a dimension of the dataset')