Flatten/ravel/collapse 3-dimensional xr.DataArray (Xarray) into 2 dimensions along an axis?
<p>I have a dataset where I'm storing replicates for different classes/subtypes (not sure what to call it) and then attributes for each one.  Essentially, there are 5 subtype/classes, 4 replicates for each subtype/class, and 100 attributes that are measured. </p>

<p><strong>Is there a method like <code>np.ravel</code> or <code>np.flatten</code> that can merge 2 dimensions using <code>Xarray</code>?</strong> </p>

<p>In this, I want to merge dims <code>subtype</code> and <code>replicates</code> so I have a 2D array (or <code>pd.DataFrame</code> with <code>attributes vs. subtype/replicates</code>. </p>

<p>It wouldn't need to have the format "coord_1 | coord_2" or anything.  It would be useful if it kept the original coord names.  Maybe there's something like <code>groupby</code> that could do this? <code>Groupby</code> always confuses me so if it's something native to <code>xarray</code> that would be awesome. </p>

<pre><code>import xarray as xr
import numpy as np

# Set up xr.DataArray
dims = (5,4,100)
DA_data = xr.DataArray(np.random.random(dims), dims=["subtype","replicates","attributes"])
DA_data.coords["subtype"] = ["subtype_%d"%_ for _ in range(dims[0])]
DA_data.coords["replicates"] = ["rep_%d"%_ for _ in range(dims[1])]
DA_data.coords["attributes"] = ["attr_%d"%_ for _ in range(dims[2])]

# DA_data.coords
# Coordinates:
#   * subtype     (subtype) &lt;U9 'subtype_0' 'subtype_1' 'subtype_2' ...
#   * replicates  (replicates) &lt;U5 'rep_0' 'rep_1' 'rep_2' 'rep_3'
#   * attributes  (attributes) &lt;U7 'attr_0' 'attr_1' 'attr_2' 'attr_3' ...
# DA_data.dims
# ('subtype', 'replicates', 'attributes')

# Naive way to collapse the replicate dimension into the subtype dimension
desired_columns = list()
for subtype in DA_data.coords["subtype"]:
    for replicate in DA_data.coords["replicates"]:
        desired_columns.append(str(subtype.values) + "|" + str(replicate.values))
desired_columns
# ['subtype_0|rep_0',
#  'subtype_0|rep_1',
#  'subtype_0|rep_2',
#  'subtype_0|rep_3',
#  'subtype_1|rep_0',
#  'subtype_1|rep_1',
#  'subtype_1|rep_2',
#  'subtype_1|rep_3',
#  'subtype_2|rep_0',
#  'subtype_2|rep_1',
#  'subtype_2|rep_2',
#  'subtype_2|rep_3',
#  'subtype_3|rep_0',
#  'subtype_3|rep_1',
#  'subtype_3|rep_2',
#  'subtype_3|rep_3',
#  'subtype_4|rep_0',
#  'subtype_4|rep_1',
#  'subtype_4|rep_2',
#  'subtype_4|rep_3']
</code></pre>