title: Writing xarray list-type attributes to netCDF
body: <p>I wish to have a list of strings as an attribute in an xarray <code>Dataset</code> that survives serialization to netCDF. I believe this is possible with the <a href="https://www.unidata.ucar.edu/software/netcdf/netcdf-4/newdocs/netcdf-c/Arrays-of-Strings.html" rel="nofollow noreferrer">NC_STRING type</a> in netCDF-4. xarray supports this but I can't get it to persist after writing to and reading from a netCDF file. After the roundtrip, the attr comes back with the list elements concatenated. I'm thinking I might have to set an encoding parameter, and although I know how to do this for variables, I don't know how for attributes.</p>
  
  <pre><code>import xarray as xr
  ds = xr.Dataset()
  ds.attrs['testing'] = ['a', 'b']
  print(ds)
  </code></pre>
  
  <p>Gives:</p>
  
  <pre><code>&lt;xarray.Dataset&gt;
  Dimensions:  ()
  Data variables:
      *empty*
  Attributes:
      testing:  ['a', 'b']
  </code></pre>
  
  <p>Now, </p>
  
  <pre><code>ds.to_netcdf('asdf.nc')
  ds = xr.open_dataset('asdf.nc', autoclose=True)
  print(ds)
  </code></pre>
  
  <p>Gives:</p>
  
  <pre><code>&lt;xarray.Dataset&gt;
  Dimensions:  ()
  Data variables:
      *empty*
  Attributes:
      testing:  ab
  </code></pre>
  
  <h2>Update</h2>
  
  <p>In response to @jhamman's comment, this behavior is supported using netCDF4's <code>netncattr_string</code> method:</p>
  
  <pre><code>import netCDF4 as nc
  rg = nc.Dataset('test_string.nc', 'w', format='NETCDF4')
  rg.setncattr_string('testing', ['a', 'b'])
  rg.close()
  </code></pre>
  
  <p>Running ncdump on test_string.nc produces:</p>
  
  <pre><code>$ ncdump test_string.nc
  netcdf test_string {
  
  // global attributes:
          string :testing = "a", "b" ;
  }
  </code></pre>
  
  <p>as opposed to setting an attribute in the usual way, which results in a concatenated attribute, as when the nc is created by xarray above:</p>
  
  <pre><code>rg = nc.Dataset('test_normal.nc', 'w', format='NETCDF4')
  rg.testing = ['a', 'b']
  rg.close()
  </code></pre>
  
  <p>ncdump results:</p>
  
  <pre><code>$ ncdump test_normal.nc
  netcdf test_normal {
  
  // global attributes:
          :testing = "ab" ;
  }
  </code></pre>
  
  <p>Reading the .nc file created using <code>setncattr_string</code> properly imports a list of strings as the attributes in an xarray Dataset:</p>
  
  <pre><code>ds = xr.open_dataset('test_string.nc')
  print(ds)
  </code></pre>
  
  <p>gives:</p>
  
  <pre><code>&lt;xarray.Dataset&gt;
  Dimensions:  ()
  Data variables:
      *empty*
  Attributes:
      testing:  ['a', 'b']
  </code></pre>
  
  <p>So I guess something like <code>setncattr_string</code> could be implemented in xarray to achieve this?</p>