title: How to count size of groups using xarray?
body: <p>I'd like to count the size of groups after grouping using <code>groupby()</code>, i.e. the number of occurrences of some value. Using pandas this can be done using <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.GroupBy.size.html" rel="nofollow noreferrer"><code>GroupBy.size()</code></a>:</p>
  <pre><code>&gt;&gt;&gt; pd.DataFrame({'my_column': [1, 1, 1, 2, 2, 3]}).groupby(by='my_column').size()                                                  
  my_column
  1    3
  2    2
  3    1
  dtype: int64
  </code></pre>
  <p>Numpy supports something similar using <code>np.unique()</code>:</p>
  <pre><code>&gt;&gt;&gt; np.unique([1, 1, 1, 2, 2, 3], return_counts=True)[1]                                                                            
  array([3, 2, 1])
  </code></pre>
  <p>Using xarray I can find only very awkward ways to achieve the same, e.g. converting the DataArray object to a Pandas DataFrame:</p>
  <pre><code>&gt;&gt;&gt; d = xr.DataArray([1, 1, 1, 2, 2, 3], name='my_column')
  &gt;&gt;&gt; d.to_dataframe().groupby(by='my_column').size()                                                                         
  my_column
  1    3
  2    2
  3    1
  dtype: int64
  </code></pre>
  <p>...or do very unreadable things like:</p>
  <pre><code>&gt;&gt;&gt; xr.ones_like(d).groupby(d).sum(dim='dim_0')                                                                                    
  &lt;xarray.DataArray 'my_column' (my_column: 3)&gt;
  array([3, 2, 1])
  Coordinates:
    * my_column  (my_column) int64 1 2 3
  </code></pre>
  <p>Is there a better way to get a reduced <code>DataArray</code> object with correct coordinates and dimensions? Is there reason for not introducing a <code>DataArrayGroupBy.size()</code> method similar to Pandas?</p>
  <p>(I was using xarray version 0.15.0 when writing this question.)</p>