Repository: PyPSA/PyPSA

Issue:
import_from_netcdf fails when importing from an XArray instance
## Checklist

- [X] I am using the current [`master`](https://github.com/pypsa/pypsa/tree/master) branch or the latest [release](https://github.com/pypsa/pypsa/releases).

## Describe the Bug

The [docs](https://pypsa.readthedocs.io/en/latest/api_reference.html#pypsa.Network.import_from_netcdf) state that `pypsa.Network.import_from_netcdf` should accept an xarray instance:

> import_from_netcdf(path, skip_time=False)
>
> Import network data from netCDF file or xarray Dataset at path.
> - path (string|xr.Dataset) – Path to netCDF dataset or instance of xarray Dataset. The string could be a URL.


However, a TypeError is raised if an XArray instance is passed. Possibly related: #381

## Minimal example:

```python
n1 = pypsa.Network()
xarr = n1.export_to_netcdf()
n2 = pypsa.Network()
n2.import_from_netcdf(xarr)
```

Gives error:

```
TypeError: expected str, bytes or os.PathLike object, not Dataset
```


Discussion:

Patch:
diff --git a/pypsa/io.py b/pypsa/io.py
index 1fbb37d5..42fe4a65 100644
--- a/pypsa/io.py
+++ b/pypsa/io.py
@@ -653,7 +653,7 @@ def import_from_netcdf(network, path, skip_time=False):
     """
     assert has_xarray, "xarray must be installed for netCDF support."
 
-    basename = Path(path).name
+    basename = "" if isinstance(path, xr.Dataset) else Path(path).name
     with ImporterNetCDF(path=path) as importer:
         _import_from_importer(network, importer, basename=basename, skip_time=skip_time)
 


Test patch:
diff --git a/test/test_bugs.py b/test/test_bugs.py
index ddb1561a..e517457b 100644
--- a/test/test_bugs.py
+++ b/test/test_bugs.py
@@ -93,3 +93,14 @@ def test_515():
     n.lopf(pyomo=False)
 
     assert n.objective == 10
+
+
+def test_779():
+    """
+    Importing from xarray dataset.
+    """
+    n1 = pypsa.Network()
+    n1.add("Bus", "bus")
+    xarr = n1.export_to_netcdf()
+    n2 = pypsa.Network()
+    n2.import_from_netcdf(xarr)