# Buggy code
def _infer_xy_labels_3d(darray, x, y, rgb):
    """
    Determine x and y labels for showing RGB images.

    Attempts to infer which dimension is RGB/RGBA by size and order of dims.

    """
    assert rgb is None or rgb != x
    assert rgb is None or rgb != y
    # Start by detecting and reporting invalid combinations of arguments
    assert darray.ndim == 3
    not_none = [a for a in (x, y, rgb) if a is not None]
    if len(set(not_none)) < len(not_none):
        raise ValueError(
            "Dimension names must be None or unique strings, but imshow was "
            "passed x=%r, y=%r, and rgb=%r." % (x, y, rgb)
        )
    for label in not_none:
        if label not in darray.dims:
            raise ValueError("%r is not a dimension" % (label,))

    # Then calculate rgb dimension if certain and check validity
    could_be_color = [
        label
        for label in darray.dims
        if darray[label].size in (3, 4) and label not in (x, y)
    ]
    if rgb is None and not could_be_color:
        raise ValueError(
            "A 3-dimensional array was passed to imshow(), but there is no "
            "dimension that could be color.  At least one dimension must be "
            "of size 3 (RGB) or 4 (RGBA), and not given as x or y."
        )
    if rgb is None and len(could_be_color) == 1:
        rgb = could_be_color[0]
    if rgb is not None and darray[rgb].size not in (3, 4):
        raise ValueError(
            "Cannot interpret dim %r of size %s as RGB or RGBA."
            % (rgb, darray[rgb].size)
        )

    # If rgb dimension is still unknown, there must be two or three dimensions
    # in could_be_color.  We therefore warn, and use a heuristic to break ties.
    if rgb is None:
        assert len(could_be_color) in (2, 3)
        rgb = could_be_color[-1]
        warnings.warn(
            "Several dimensions of this array could be colors.  Xarray "
            "will use the last possible dimension (%r) to match "
            "matplotlib.pyplot.imshow.  You can pass names of x, y, "
            "and/or rgb dimensions to override this guess." % rgb
        )
    assert rgb is not None

    # Finally, we pick out the red slice and delegate to the 2D version:
    return _infer_xy_labels(darray.isel(**{rgb: 0}).squeeze(), x, y)


# Traceback (ValueError)
/home/mowglie/Documents/git/xarray/xarray/plot/utils.py:295: UserWarning: Several dimensions of this array could be colors.  Xarray will use the last possible dimension ('dim_2') to match matplotlib.pyplot.imshow.  You can pass names of x, y, and/or rgb dimensions to override this guess.
'and/or rgb dimensions to override this guess.' % rgb)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-a0592d3e8758> in <module>()
----> 1 da.plot.imshow()

~/Documents/git/xarray/xarray/plot/plot.py in plotmethod(_PlotMethods_obj, x, y, figsize, size, aspect, ax, row, col, col_wrap, xincrease, yincrease, add_colorbar, add_labels, vmin, vmax, cmap, colors, center, robust, extend, levels, infer_intervals, subplot_kws, cbar_ax, cbar_kwargs, **kwargs)
679         for arg in ['_PlotMethods_obj', 'newplotfunc', 'kwargs']:
680             del allargs[arg]
--> 681         return newplotfunc(**allargs)
682
683     # Add to class _PlotMethods

~/Documents/git/xarray/xarray/plot/plot.py in newplotfunc(darray, x, y, figsize, size, aspect, ax, row, col, col_wrap, xincrease, yincrease, add_colorbar, add_labels, vmin, vmax, cmap, center, robust, extend, levels, infer_intervals, colors, subplot_kws, cbar_ax, cbar_kwargs, **kwargs)
553         rgb = kwargs.pop('rgb', None)
554         xlab, ylab = _infer_xy_labels(
--> 555             darray=darray, x=x, y=y, imshow=imshow_rgb, rgb=rgb)
556
557         if rgb is not None and plotfunc.__name__ != 'imshow':

~/Documents/git/xarray/xarray/plot/utils.py in _infer_xy_labels(darray, x, y, imshow, rgb)
308     assert x is None or x != y
309     if imshow and darray.ndim == 3:
--> 310         return _infer_xy_labels_3d(darray, x, y, rgb)
311
312     if x is None and y is None:

~/Documents/git/xarray/xarray/plot/utils.py in _infer_xy_labels_3d(darray, x, y, rgb)
297
298     # Finally, we pick out the red slice and delegate to the 2D version:
--> 299     return _infer_xy_labels(darray.isel(**{rgb: 0}).squeeze(), x, y)
300
301

~/Documents/git/xarray/xarray/plot/utils.py in _infer_xy_labels(darray, x, y, imshow, rgb)
312     if x is None and y is None:
313         if darray.ndim != 2:
--> 314             raise ValueError('DataArray must be 2d')
315         y, x = darray.dims
316     elif x is None:

ValueError: DataArray must be 2d

# Fixed code
def _infer_xy_labels_3d(darray, x, y, rgb):
    """
    Determine x and y labels for showing RGB images.

    Attempts to infer which dimension is RGB/RGBA by size and order of dims.

    """
    assert rgb is None or rgb != x
    assert rgb is None or rgb != y
    # Start by detecting and reporting invalid combinations of arguments
    assert darray.ndim == 3
    not_none = [a for a in (x, y, rgb) if a is not None]
    if len(set(not_none)) < len(not_none):
        raise ValueError(
            "Dimension names must be None or unique strings, but imshow was "
            "passed x=%r, y=%r, and rgb=%r." % (x, y, rgb)
        )
    for label in not_none:
        if label not in darray.dims:
            raise ValueError("%r is not a dimension" % (label,))

    # Then calculate rgb dimension if certain and check validity
    could_be_color = [
        label
        for label in darray.dims
        if darray[label].size in (3, 4) and label not in (x, y)
    ]
    if rgb is None and not could_be_color:
        raise ValueError(
            "A 3-dimensional array was passed to imshow(), but there is no "
            "dimension that could be color.  At least one dimension must be "
            "of size 3 (RGB) or 4 (RGBA), and not given as x or y."
        )
    if rgb is None and len(could_be_color) == 1:
        rgb = could_be_color[0]
    if rgb is not None and darray[rgb].size not in (3, 4):
        raise ValueError(
            "Cannot interpret dim %r of size %s as RGB or RGBA."
            % (rgb, darray[rgb].size)
        )

    # If rgb dimension is still unknown, there must be two or three dimensions
    # in could_be_color.  We therefore warn, and use a heuristic to break ties.
    if rgb is None:
        assert len(could_be_color) in (2, 3)
        rgb = could_be_color[-1]
        warnings.warn(
            "Several dimensions of this array could be colors.  Xarray "
            "will use the last possible dimension (%r) to match "
            "matplotlib.pyplot.imshow.  You can pass names of x, y, "
            "and/or rgb dimensions to override this guess." % rgb
        )
    assert rgb is not None

    # Finally, we pick out the red slice and delegate to the 2D version:
    return _infer_xy_labels(darray.isel(**{rgb: 0}), x, y)