# Combining a set of 2D variables into a 3D DataArray

**URL:** <https://discourse.pangeo.io/t/combining-a-set-of-2d-variables-into-a-3d-dataarray/1647>\
**Category:** Uncategorized\
**Created:** [July 18, 2021, 11:10pm UTC](https://discourse.pangeo.io/t/combining-a-set-of-2d-variables-into-a-3d-dataarray/1647 "2021-07-18T23:10:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tedhabermann](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/tedhabermann/32/1047_2.png) [@tedhabermann](https://discourse.pangeo.io/u/tedhabermann)\
**Post date:** [July 18, 2021, 11:10pm UTC](https://discourse.pangeo.io/t/combining-a-set-of-2d-variables-into-a-3d-dataarray/1647/1 "2021-07-18T23:10:15Z")

</div>

I have a simple situation, a dataset made up of 50 data arrays all with two dimensions that are the same for all dataArrays (time, satellite) and both dimensions have the same values in each data array. Each of these data arrays has a name which is a string, i.e ‘C2C’. I would like to combine these into a 3D dataset with the outside dimension being the variable name from the original dataset. I have read merge and concat but can not get either to work…

---

<div class="post-metadata">

**Author:** ![rabernat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/rabernat/32/22_2.png) [@rabernat](https://discourse.pangeo.io/u/rabernat)\
**Post date:** [July 19, 2021, 8:03am UTC](https://discourse.pangeo.io/t/combining-a-set-of-2d-variables-into-a-3d-dataarray/1647/2 "2021-07-19T08:03:54Z")

</div>

You are on the right track. You want to use Xarray’s [concat](http://xarray.pydata.org/en/stable/user-guide/combining.html) feature. Here is a simple example.

```python
import xarray as xr
foo = xr.DataArray([1, 2], dims='time', name='foo')
bar = xr.DataArray([3, 4], dims='time', name='bar')
# the trick: create a new DataArray to represent the new dimension
name_da = xr.DataArray([da.name for da in (foo, bar)], name='varname')
combined = xr.concat([foo, bar], dim=name_da)

```

---

<div class="post-metadata">

**Author:** ![tedhabermann](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/tedhabermann/32/1047_2.png) [@tedhabermann](https://discourse.pangeo.io/u/tedhabermann)\
**Post date:** [July 20, 2021, 2:48pm UTC](https://discourse.pangeo.io/t/combining-a-set-of-2d-variables-into-a-3d-dataarray/1647/3 "2021-07-20T14:48:16Z")

</div>

Ryan,

Hmmm - there is a trick! I will give this a try soon.

Thanks,  
Ted

---

<div class="post-metadata">

**Author:** ![rabernat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/rabernat/32/22_2.png) [@rabernat](https://discourse.pangeo.io/u/rabernat)\
**Post date:** [July 20, 2021, 2:59pm UTC](https://discourse.pangeo.io/t/combining-a-set-of-2d-variables-into-a-3d-dataarray/1647/4 "2021-07-20T14:59:32Z")

</div>

The trick is not a dealbreaker. You could also just do `xr.concat(..., dim="varname")` but then you not have a labeled coordiante for the varname dimension.

---

<div class="post-metadata">

**Author:** ![tedhabermann](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/tedhabermann/32/1047_2.png) [@tedhabermann](https://discourse.pangeo.io/u/tedhabermann)\
**Post date:** [July 23, 2021, 6:04pm UTC](https://discourse.pangeo.io/t/combining-a-set-of-2d-variables-into-a-3d-dataarray/1647/5 "2021-07-23T18:04:37Z")

</div>

Ryan,

Thanks again for the pointer. I ended up getting the desired result with  
station = xr.concat(obs.values(),dim=list(obs.data\_vars)).  
rename({‘concat\_dim’:‘observation’}).  
rename(‘observations’)  
where obs is the original dataSet and station is the new dataset…

Ted
