# xarray.Dataset.to_stacked_array

#### Dataset.to_stacked_array(new_dim, sample_dims, variable_dim='variable', name=None)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L5421-L5542)

Combine variables of differing dimensionality into a DataArray
without broadcasting.

This method is similar to Dataset.to_dataarray but does not broadcast the
variables.

* **Parameters:**
  * **new_dim** ([hashable](../user-guide/terminology.html.md#term-name)) – Name of the new stacked coordinate
  * **sample_dims** (`Collection` of `hashables`) – List of dimensions that **will not** be stacked. Each array in the
    dataset must share these dimensions. For machine learning
    applications, these define the dimensions over which samples are
    drawn.
  * **variable_dim** ([hashable](../user-guide/terminology.html.md#term-name), *default*: `"variable"`) – Name of the level in the stacked coordinate which corresponds to
    the variables.
  * **name** ([hashable](../user-guide/terminology.html.md#term-name), *optional*) – Name of the new data array.
* **Returns:**
  **stacked** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – DataArray with the specified dimensions and data variables
  stacked together. The stacked coordinate is named `new_dim`
  and represented by a MultiIndex object with a level containing the
  data variable names. The name of this level is controlled using
  the `variable_dim` argument.

#### SEE ALSO
[`Dataset.to_dataarray`](xarray.Dataset.to_dataarray.html.md#xarray.Dataset.to_dataarray), [`Dataset.stack`](xarray.Dataset.stack.html.md#xarray.Dataset.stack), [`DataArray.to_unstacked_dataset`](xarray.DataArray.to_unstacked_dataset.html.md#xarray.DataArray.to_unstacked_dataset)

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('829dc201-51df-4c74-a49c-d77b28e6c5b0','b59056b3-56f8-45e2-a64d-792c1089a8e5','f8952e41-d63f-4cdb-a3aa-c866ec6a6cd5','../lite/tree/../notebooks/index.html?path=ba4430b5_d47d_4c24_a44c_79356a71551f.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> data = xr.Dataset(
...     data_vars={
...         "a": (("x", "y"), [[0, 1, 2], [3, 4, 5]]),
...         "b": ("x", [6, 7]),
...     },
...     coords={"y": ["u", "v", "w"]},
... )
```

```pycon
>>> data
<xarray.Dataset> Size: 76B
Dimensions:  (x: 2, y: 3)
Coordinates:
  * y        (y) <U1 12B 'u' 'v' 'w'
Dimensions without coordinates: x
Data variables:
    a        (x, y) int64 48B 0 1 2 3 4 5
    b        (x) int64 16B 6 7
```

```pycon
>>> data.to_stacked_array("z", sample_dims=["x"])
<xarray.DataArray 'a' (x: 2, z: 4)> Size: 64B
array([[0, 1, 2, 6],
       [3, 4, 5, 7]])
Coordinates:
  * z         (z) object 32B MultiIndex
  * variable  (z) <U1 16B 'a' 'a' 'a' 'b'
  * y         (z) object 32B 'u' 'v' 'w' nan
Dimensions without coordinates: x
```

<div id="f8952e41-d63f-4cdb-a3aa-c866ec6a6cd5" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('829dc201-51df-4c74-a49c-d77b28e6c5b0','f8952e41-d63f-4cdb-a3aa-c866ec6a6cd5')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('829dc201-51df-4c74-a49c-d77b28e6c5b0','f8952e41-d63f-4cdb-a3aa-c866ec6a6cd5')">Open In Tab</button></div><div id="b59056b3-56f8-45e2-a64d-792c1089a8e5" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
