Creating output variables and copying attributes in python xarray netcdf4
<p>I can create variables and copy over attributes in netcdf4 like this:</p>

<pre><code>out_var = hndl_out_nc.createVariable(name_var, var.datatype, var.dimensions)
out_var.setncatts({k: var.getncattr(k) for k in var.ncattrs()})
</code></pre>

<p>What is the corresponding version for xarray?</p>

<p>If <code>var</code> is an <code>xarray.DataArray</code>, you can put it (along with attributes) into a new <code>xarray.Dataset</code> simply by writing <code>ds[name_] = var</code>. Or you can construct a new DataArray piece by piece with <code>xarray.DataArray(var.data, var.coords, var.dims, var.attrs)</code>.</p>

python python-xarray netcdf4