import numpy as np
import xarray as xr
import datetime
data = xr.DataArray(np.random.randn(2, 3), coords={'x': ['a', 'b']}, dims=('x', 'y'))
ds = xr.Dataset({'z': data})
ds.to_netcdf('netcdf_test.nc') # works
ds.attrs['created'] = datetime.datetime.now()
ds.to_netcdf('netcdf_test_time.nc') # fails
I thought about opening this issue upstream, but their solution for handling datetimes as DataArrays is precisely to use the attrs as metadata for that array. I don't think they would support generic serialization of datetimes that are in attrs.
It seems like we should either not use datetimes or just serialize/deserialize ourselves.