# xarray.DataArray.stack

#### DataArray.stack(dim=None, create_index=True, index_cls=<class 'xarray.core.indexes.PandasMultiIndex'>, \*\*dim_kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataarray.py#L3049-L3121)

Stack any number of existing dimensions into a single new dimension.

New dimensions will be added at the end, and the corresponding
coordinate variables will be combined into a MultiIndex.

* **Parameters:**
  * **dim** ([mapping](https://docs.python.org/3/glossary.html#term-mapping) of `Hashable` to [sequence](https://docs.python.org/3/glossary.html#term-sequence) of `Hashable`) – Mapping of the form new_name=(dim1, dim2, …).
    Names of new dimensions, and the existing dimensions that they
    replace. An ellipsis (…) will be replaced by all unlisted dimensions.
    Passing a list containing an ellipsis (stacked_dim=[…]) will stack over
    all dimensions.
  * **create_index** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool) or [`None`](https://docs.python.org/3/builtins/constants.html#None), *default*: [`True`](https://docs.python.org/3/builtins/constants.html#True)) – If True, create a multi-index for each of the stacked dimensions.
    If False, don’t create any index.
    If None, create a multi-index only if exactly one single (1-d) coordinate
    index is found for every dimension to stack.
  * **index_cls** (`class`, *optional*) – Can be used to pass a custom multi-index type. Must be an Xarray index that
    implements .stack(). By default, a pandas multi-index wrapper is used.
  * **\*\*dim_kwargs** – The keyword arguments form of `dim`.
    One of dim or dim_kwargs must be provided.
* **Returns:**
  **stacked** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – DataArray with stacked data.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('f79c6e01-44ba-4375-9332-1f3d7343c9dc','31c009fa-daab-4cf8-9e4d-e6eff817ecd5','8f576560-9819-4afb-8dc3-895618599269','../lite/tree/../notebooks/index.html?path=ff093766_f6eb_497e_9cc1_28f2cf9efdb8.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])],
... )
>>> arr
<xarray.DataArray (x: 2, y: 3)> Size: 48B
array([[0, 1, 2],
       [3, 4, 5]])
Coordinates:
  * x        (x) <U1 8B 'a' 'b'
  * y        (y) int64 24B 0 1 2
>>> stacked = arr.stack(z=("x", "y"))
>>> stacked.indexes["z"]
MultiIndex([('a', 0),
            ('a', 1),
            ('a', 2),
            ('b', 0),
            ('b', 1),
            ('b', 2)],
           name='z')
```

<div id="8f576560-9819-4afb-8dc3-895618599269" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('f79c6e01-44ba-4375-9332-1f3d7343c9dc','8f576560-9819-4afb-8dc3-895618599269')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('f79c6e01-44ba-4375-9332-1f3d7343c9dc','8f576560-9819-4afb-8dc3-895618599269')">Open In Tab</button></div><div id="31c009fa-daab-4cf8-9e4d-e6eff817ecd5" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

#### SEE ALSO
[`DataArray.unstack`](xarray.DataArray.unstack.html.md#xarray.DataArray.unstack)
