Repository: xarray-contrib/cf-xarray

Issue:
"name" custom criteria not working for DataArray
```python
import cf_xarray
import xarray as xr

da = xr.DataArray([1, 2], coords={"lat": [3, 4]})
lat_criteria = {"latitude": {"name": "lat"}}
cf_xarray.set_options(custom_criteria=lat_criteria)
da.cf["latitude"]
```

Raises

```python-traceback
File ~/code/cf-xarray/cf_xarray/accessor.py:223, in _get_custom_criteria(obj, key, criteria)
    219                 results.update((var,))
    220             # also check name specifically since not in attributes
--> 223             elif criterion == "name" and re.match(patterns, var):
    224                 results.update((var,))
    225 return list(results)

File ~/.conda/envs/cf_xarray_dev/lib/python3.11/re/__init__.py:166, in match(pattern, string, flags)
    163 def match(pattern, string, flags=0):
    164     """Try to apply the pattern at the start of the string, returning
    165     a Match object, or None if no match was found."""
--> 166     return _compile(pattern, flags).match(string)

TypeError: expected string or bytes-like object, got 'ReprObject'
```

because the DataArray is converted to a temporary Dataset which uses a `ReprObject` as its name. An easy fix is to call `str` on `var` here:

https://github.com/xarray-contrib/cf-xarray/blob/3795af6cb6f5d37ffac7e2a67f5b4ead1255b202/cf_xarray/accessor.py#L217




Discussion:

Patch:
diff --git a/cf_xarray/accessor.py b/cf_xarray/accessor.py
index 8cabbb0..f6d4ad6 100644
--- a/cf_xarray/accessor.py
+++ b/cf_xarray/accessor.py
@@ -214,7 +214,11 @@ def _get_custom_criteria(
                 if re.match(patterns, obj[var].attrs.get(criterion, "")):
                     results.update((var,))
                 # also check name specifically since not in attributes
-                elif criterion == "name" and re.match(patterns, var):
+                elif (
+                    criterion == "name"
+                    and isinstance(var, str)
+                    and re.match(patterns, var)
+                ):
                     results.update((var,))
     return list(results)
 
diff --git a/doc/whats-new.rst b/doc/whats-new.rst
index 6395432..1de7a13 100644
--- a/doc/whats-new.rst
+++ b/doc/whats-new.rst
@@ -6,7 +6,7 @@ What's New
 v0.7.6 (unreleased)
 ===================
 
-
+- Allow custom criteria to match the variable name of DataArray objects (:pr:`379`). By `Mathias Hauser`_
 
 v0.7.5 (Nov 15, 2022)
 =====================
@@ -190,4 +190,5 @@ v0.1.3
 .. _`Lars Buntemeyer`: https://github.com/larsbuntemeyer
 .. _`Luke Davis`: https://github.com/lukelbd
 .. _`Martin Schupfner`: https://github.com/sol1105
+.. _`Mathias Hauser`: https://github.com/mathause
 .. _`flag variables`: http://cfconventions.org/Data/cf-conventions/cf-conventions-1.8/cf-conventions.html#flags


Test patch:
diff --git a/cf_xarray/tests/test_accessor.py b/cf_xarray/tests/test_accessor.py
index a1cb2a5..103e3b3 100644
--- a/cf_xarray/tests/test_accessor.py
+++ b/cf_xarray/tests/test_accessor.py
@@ -1498,6 +1498,10 @@ def test_custom_criteria():
     ds["salinity"] = ("dim", np.arange(10))
     assert_identical(ds.cf["salt"], ds["salinity"])
 
+    # match by name of coords for DataArray (#378)
+    da = xr.DataArray([0, 1], coords={"salinity": [2, 3]})
+    assert_identical(da.cf["salt"], da["salinity"])
+
     # Match by standard_name regex match
     ds = xr.Dataset()
     ds["elev"] = ("dim", np.arange(10), {"standard_name": "sea_surface_elevBLAH"})