id,node_id,number,title,user,state,locked,assignee,milestone,comments,created_at,updated_at,closed_at,author_association,active_lock_reason,draft,pull_request,body,reactions,performed_via_github_app,state_reason,repo,type 181099871,MDU6SXNzdWUxODEwOTk4NzE=,1039,NetCDF4 backend fails with single-value dimension,5184063,closed,0,1217238,,1,2016-10-05T09:03:52Z,2017-06-17T00:14:32Z,2017-06-17T00:14:32Z,NONE,,,,"Exporting and xarray object with a string dimension which only consists of a single value fails using python 3 and xarray 0.8.2. Here is an example to reproduce the error using the toy weather data from the tutorials: ``` python import xarray as xr import numpy as np import pandas as pd times = pd.date_range('2000-01-01', '2001-12-31', name='time') annual_cycle = np.sin(2 * np.pi * (times.dayofyear / 365.25 - 0.28)) base = 10 + 15 * annual_cycle.reshape(-1, 1) tmin_values = base + 3 * np.random.randn(annual_cycle.size, 3) tmax_values = base + 10 + 3 * np.random.randn(annual_cycle.size, 3) ds = xr.Dataset({'tmin': (('time', 'location'), tmin_values), 'tmax': (('time', 'location'), tmax_values)}, {'time': times, 'location': ['IA', 'IN', 'IL']}) ``` The following function call fails for me with a `TypeError: len() of unsized object`: ``` python ds.sel(location=""IA"").to_netcdf(""asdf"", engine=""netcdf4"") ``` The lower part of the traceback: ``` pytb D:\Anaconda3\lib\site-packages\xarray\backends\netCDF4_.py in prepare_variable(self, name, variable, check_encoding) 250 251 if self.format == 'NETCDF4': --> 252 variable, datatype = _nc4_values_and_dtype(variable) 253 else: 254 variable = encode_nc3_variable(variable) D:\Anaconda3\lib\site-packages\xarray\backends\netCDF4_.py in _nc4_values_and_dtype(var) 75 if var.dtype.kind == 'U': 76 # this entire clause should not be necessary with netCDF4>=1.0.9 ---> 77 if len(var) > 0: 78 var = var.astype('O') 79 dtype = str D:\Anaconda3\lib\site-packages\xarray\core\utils.py in __len__(self) 371 return self.shape[0] 372 except IndexError: --> 373 raise TypeError('len() of unsized object') 374 375 ``` The export works fine both without the selection: ``` python ds.to_netcdf(""asdf"", engine=""netcdf4"") ``` And with dropping the dimension after: ``` python ds.sel(location=""IA"").drop(""location"").to_netcdf(""asdf"", engine=""netcdf4"") ``` ","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/1039/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,completed,13221727,issue