xarray dataset from dataarray code example

Example 1: 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')

Example 2: how to open a dataset in xarray

import xarray as xr

#opens the dataset
ds = xr.open_dataset(/path_to_file/name_of_file.nc)