# xarray.DataArray.to_unstacked_dataset

#### DataArray.to_unstacked_dataset(dim, level=0)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataarray.py#L3192-L3254)

Unstack DataArray to a Dataset along a level of a stacked coordinate.

This is the inverse operation of Dataset.to_stacked_array.

* **Parameters:**
  * **dim** (`Hashable`) – Name of existing dimension to unstack
  * **level** ([`int`](https://docs.python.org/3/builtins/functions.html#int) or `Hashable`, *default*: `0`) – The MultiIndex level to expand to a dataset along. Can either be
    the integer index of the level or its name.
* **Returns:**
  **unstacked** ([`Dataset`](xarray.Dataset.html.md#xarray.Dataset))

#### SEE ALSO
[`Dataset.to_stacked_array`](xarray.Dataset.to_stacked_array.html.md#xarray.Dataset.to_stacked_array)

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('fda96645-8b67-45cf-990f-809b531fd67d','f549b67a-fd5d-4ac9-a2cb-d0d11f105f7e','79a1edf3-981c-4f14-9b8d-4e1a9fde39a5','../lite/tree/../notebooks/index.html?path=0876fe59_e7c0_490c_ad69_1e6424feae76.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> arr = xr.DataArray(
...     np.arange(6).reshape(2, 3),
...     coords=[("x", ["a", "b"]), ("y", [0, 1, 2])],
... )
>>> data = xr.Dataset({"a": arr, "b": arr.isel(y=0)})
>>> data
<xarray.Dataset> Size: 96B
Dimensions:  (x: 2, y: 3)
Coordinates:
  * x        (x) <U1 8B 'a' 'b'
  * y        (y) int64 24B 0 1 2
Data variables:
    a        (x, y) int64 48B 0 1 2 3 4 5
    b        (x) int64 16B 0 3
>>> stacked = data.to_stacked_array("z", ["x"])
>>> stacked.indexes["z"]
MultiIndex([('a', 0.0),
            ('a', 1.0),
            ('a', 2.0),
            ('b', nan)],
           name='z')
>>> roundtripped = stacked.to_unstacked_dataset(dim="z")
>>> data.identical(roundtripped)
True
```

<div id="79a1edf3-981c-4f14-9b8d-4e1a9fde39a5" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('fda96645-8b67-45cf-990f-809b531fd67d','79a1edf3-981c-4f14-9b8d-4e1a9fde39a5')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('fda96645-8b67-45cf-990f-809b531fd67d','79a1edf3-981c-4f14-9b8d-4e1a9fde39a5')">Open In Tab</button></div><div id="f549b67a-fd5d-4ac9-a2cb-d0d11f105f7e" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
