How to flatten an xarray dataset into a 1D numpy array?

If you're OK with repeated values, you can use .to_array() and then flatten the values in NumPy, e.g.,

>>> ds.to_array().values.ravel()
array([10, 11, 12, 13, 14,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  3,  3,
        3,  3,  3])

If you don't want repeated values, then you'll need to write something yourself, e.g.,

>>> np.concatenate([v.values.ravel() for v in ds.data_vars.values()])
array([10, 11, 12, 13, 14,  1,  2,  3])

More generally, this sounds somewhat similar to a proposed interface for "stacking" data variables in 2D for machine learning applications: https://github.com/pydata/xarray/issues/1317


As of July 2019, xarray now has the functions to_stacked_array and to_unstacked_dataset that perform this function.