Is it possible to create a pytree (for JAX) based upon an xarray.DataArray?
The key issue seems to be that the data attribute of an xarray.DataArray is constructed as a numpy.ndarray view of the input array (such as DeviceArray)
import jax.numpy as jnp
import xarray as xr
da = xr.DataArray(
data=jnp.array([2.0,3.0]),
dims=("var"),
coords={"var": ["A","B"]},
)
>>> type(da.data)
numpy.ndarray
Flattening/Unflattening the DataArray into a pytree is relatively straight-forward (all the attributes are aux except data), but I don't know how to retrieve the DeviceArray, or even assign to it (I can't use at[.].set(.) here).
The alternative approach of constructing a container class (with DeviceArray member) requires that all the relevant functionality of xarray be manually implemented. For a single feature, such as labelled indexing, this is possible but redundant.