html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue https://github.com/pydata/xarray/issues/7186#issuecomment-1287864880,https://api.github.com/repos/pydata/xarray/issues/7186,1287864880,IC_kwDOAMm_X85Mw0Iw,57705593,2022-10-22T17:37:05Z,2022-10-22T17:37:41Z,CONTRIBUTOR,"The reason for this behavior is that the `netcdf4` python package automatically determines the type of the attribute (`NC_CHAR` or `NC_STRING`) by attempting the conversion to ASCII: https://github.com/Unidata/netcdf4-python/issues/529 However, if the value is a byte string, no conversion is done. So, the easiest solution would be to manually encode as `utf-8` and then passing the byte string to `netcdf4`. Unfortunately, `xarray` doesn't support byte strings as attribute values even though this is a valid data type for the `netcdf4` engine: https://github.com/pydata/xarray/blob/6cb97f645475bddf2f3b1e1a5f24f0f9de690683/xarray/backends/api.py#L175 In the long term, I would suggest to add `bytes` as a supported type in that list above on xarray's side. A quick workaround for you might be to encode the string as `utf-8` and convert to a numpy array, since xarray accepts numpy arrays as data type and `netcdf4` automatically extracts the data if the array contains only a single item: ```python ds[""x""].attrs[""third_str""] = np.array(""hää"".encode(""utf-8"")) ```","{""total_count"": 1, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 1, ""rocket"": 0, ""eyes"": 0}",,1414669747