Repository: yt-project/unyt

Issue:
unyt fails to convert dimensionless array to astropy using .to_astropy()
* unyt version: 2.9.3
* Python version: 3.8.10 
* Operating System: Ubuntu (WSL)

### Description

When converting a dimensionless ``unyt_array`` to an astropy compatible array, ``unyt_array.to_astrpy()`` raises 
```
ValueError: 'dimensionless' did not parse as unit: At col 0, dimensionless is not a valid unit. 
If this is meant to be a custom unit, define it with 'u.def_unit'. 
To have it recognized inside a file reader or other code, enable it with 'u.add_enabled_units'. For details, see 
https://docs.astropy.org/en/latest/units/combining_and_defining.html
```

It would be worthwhile to circumvent this exception in the ``.to_astropy()`` function to avoid any issues here.

### What I Did

Replicable example

```
from unyt import unyt_array
import numpy as np

x,y = unyt_array([1,2,3],"kpc"),unyt_array([1,1,1],"kpc")
z = x/y

q = z.to_astropy()

--------------------------------------------------------------------
/home/ediggins/.virtualenvs/cluster_generator/bin/python /mnt/c/Users/13852/AppData/Roaming/JetBrains/PyCharm2023.2/scratches/scratch_1.py 
Traceback (most recent call last):
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/units/format/generic.py", line 616, in _do_parse
    return cls._parse_unit(s, detailed_exception=False)
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/units/format/generic.py", line 512, in _parse_unit
    raise ValueError()
ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/units/format/generic.py", line 482, in _get_unit
    return cls._parse_unit(t.value)
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/units/format/generic.py", line 510, in _parse_unit
    raise ValueError(f"{s} is not a valid unit. {did_you_mean(s, registry)}")
ValueError: dimensionless is not a valid unit. 

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/units/core.py", line 2072, in __call__
    return f.parse(s)
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/units/format/generic.py", line 600, in parse
    result = cls._do_parse(s, debug=debug)
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/units/format/generic.py", line 619, in _do_parse
    return cls._parser.parse(s, lexer=cls._lexer, debug=debug)
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/utils/parsing.py", line 116, in parse
    return self.parser.parse(*args, **kwargs)
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/extern/ply/yacc.py", line 333, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/extern/ply/yacc.py", line 1063, in parseopt_notrack
    lookahead = get_token()     # Get the next token
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/extern/ply/lex.py", line 350, in token
    newtok = func(tok)
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/units/format/generic.py", line 152, in t_UNIT
    t.value = cls._get_unit(t)
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/units/format/generic.py", line 488, in _get_unit
    raise ValueError(f"At col {t.lexpos}, {str(e)}")
ValueError: At col 0, dimensionless is not a valid unit. 

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/mnt/c/Users/13852/AppData/Roaming/JetBrains/PyCharm2023.2/scratches/scratch_1.py", line 7, in <module>
    q = z.to_astropy()
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/unyt/array.py", line 1296, in to_astropy
    return self.value * _astropy.units.Unit(str(self.units), **kwargs)
  File "/home/ediggins/.virtualenvs/cluster_generator/lib/python3.8/site-packages/astropy/units/core.py", line 2095, in __call__
    raise ValueError(msg)
ValueError: 'dimensionless' did not parse as unit: At col 0, dimensionless is not a valid unit.  If this is meant to be a custom unit, define it with 'u.def_unit'. To have it recognized inside a file reader or other code, enable it with 'u.add_enabled_units'. For details, see https://docs.astropy.org/en/latest/units/combining_and_defining.html
```


Discussion:

Patch:
diff --git a/unyt/array.py b/unyt/array.py
index ef06fb4..913850b 100644
--- a/unyt/array.py
+++ b/unyt/array.py
@@ -1313,7 +1313,11 @@ class unyt_array(np.ndarray):
         >>> data.to_astropy()
         <Quantity [3., 4., 5.] g / cm3>
         """
-        return self.value * _astropy.units.Unit(str(self.units), **kwargs)
+        if self.units.is_dimensionless:
+            s_units = ""
+        else:
+            s_units = str(self.units)
+        return self.value * _astropy.units.Unit(s_units, **kwargs)
 
     @classmethod
     def from_pint(cls, arr, unit_registry=None):


Test patch:
diff --git a/unyt/tests/test_unyt_array.py b/unyt/tests/test_unyt_array.py
index e61db46..8ab0af9 100644
--- a/unyt/tests/test_unyt_array.py
+++ b/unyt/tests/test_unyt_array.py
@@ -1402,6 +1402,17 @@ def test_astropy():
     assert_equal(yt_quan, unyt_quantity.from_astropy(yt_quan.to_astropy()))
 
 
+def test_astropy_dimensionless():
+    # see https://github.com/yt-project/unyt/issues/436
+    pytest.importorskip("astropy")
+
+    arr = unyt_array([1, 2, 3], "")
+    ap_arr = np.array([1, 2, 3]) * _astropy.units.Unit("")
+
+    assert_array_equal(ap_arr, arr.to_astropy())
+    assert_array_equal(arr, unyt_array.from_astropy(ap_arr))
+
+
 def test_pint():
     pytest.importorskip("pint")