Repository: pydata/xarray (Python)

Issue:
dataset encodings 'source' and 'original_shape' are not dropped in `zarr` backend
### What happened?

When opening a dataset, like one from the tutorial, and writing it as zarr file, an error is raised due to encodings that are invalid for the `zarr` driver, when the encoding is given in `to_zarr`. In this particular case, the encodings `source` and `original_shape` are added by xarray itself, so that I expect that it can handle these encodings without raising an error.

### What did you expect to happen?

I expect that the encodings `source` and `original_shape` being dropped similar to the `netCDF4` backend. 

### Minimal Complete Verifiable Example

```Python
import xarray as xr
ds = xr.tutorial.load_dataset("eraint_uvz")
ds.to_zarr("test.zarr", encoding={"z":{**ds.z.encoding}})
```


### MVCE confirmation

- [X] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- [X] Complete example — the example is self-contained, including all data and the text of any traceback.
- [X] Verifiable example — the example copy & pastes into an IPython prompt or [Binder notebook](https://mybinder.org/v2/gh/pydata/xarray/main?urlpath=lab/tree/doc/examples/blank_template.ipynb), returning the result.
- [X] New issue — a search of GitHub Issues suggests this is not a duplicate.

### Relevant log output

```Python
>>> ds.to_zarr("test_w_offset.zarr01", encoding={"z":{**ds.z.encoding}})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../.conda/envs/xarray2022090/lib/python3.10/site-packages/xarray/core/dataset.py", line 2068, in to_zarr
    return to_zarr(  # type: ignore
  File ".../.conda/envs/xarray2022090/lib/python3.10/site-packages/xarray/backends/api.py", line 1653, in to_zarr
    dump_to_store(dataset, zstore, writer, encoding=encoding)
  File ".../.conda/envs/xarray2022090/lib/python3.10/site-packages/xarray/backends/api.py", line 1273, in dump_to_store
    store.store(variables, attrs, check_encoding, writer, unlimited_dims=unlimited_dims)
  File ".../.conda/envs/xarray2022090/lib/python3.10/site-packages/xarray/backends/zarr.py", line 574, in store
    self.set_variables(
  File ".../.conda/envs/xarray2022090/lib/python3.10/site-packages/xarray/backends/zarr.py", line 621, in set_variables
    encoding = extract_zarr_variable_encoding(
  File ".../.conda/envs/xarray2022090/lib/python3.10/site-packages/xarray/backends/zarr.py", line 247, in extract_zarr_variable_encoding
    raise ValueError(
ValueError: unexpected encoding parameters for zarr backend:  ['source', 'original_shape']
```


### Anything else we need to know?

The respective lines in the netCDF4 backend are:
https://github.com/pydata/xarray/blob/13c52b27b777709fc3316cf4334157f50904c02b/xarray/backends/netCDF4_.py#L235
and

https://github.com/pydata/xarray/blob/13c52b27b777709fc3316cf4334157f50904c02b/xarray/backends/netCDF4_.py#L272-L274


### Environment

<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:35:26) [GCC 10.4.0]
python-bits: 64
OS: Linux
OS-release: 4.18.0-305.25.1.el8_4.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.2
libnetcdf: 4.8.1

xarray: 2022.9.0
pandas: 1.5.0
numpy: 1.23.3
scipy: None
netCDF4: 1.6.1
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: 2.13.2
cftime: 1.6.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 65.4.1
pip: 22.2.2
conda: None
pytest: None
IPython: 8.3.0
sphinx: None



</details>



Discussion: Yes this seems like a bug and should be easy to fix. PRs welcome!



Fix (patch):
diff --git a/doc/whats-new.rst b/doc/whats-new.rst
index 26bd72b0727..458765e9d70 100644
--- a/doc/whats-new.rst
+++ b/doc/whats-new.rst
@@ -55,6 +55,9 @@ Bug fixes
   By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_ and `Scott Chamberlin <https://github.com/scottcha>`_.
 - Handle ``keep_attrs`` option in binary operators of :py:meth:`Dataset` (:issue:`7390`, :pull:`7391`).
   By `Aron Gergely <https://github.com/arongergely>`_.
+- :py:func:`xarray.Dataset.to_zarr` now drops variable encodings that have been added by xarray during reading
+  a dataset. (:issue:`7129`, :pull:`7500`).
+  By `Hauke Schulz <https://github.com/observingClouds>`_.
 
 Documentation
 ~~~~~~~~~~~~~
diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py
index e30e7e9f4d8..ae400c2b340 100644
--- a/xarray/backends/zarr.py
+++ b/xarray/backends/zarr.py
@@ -230,6 +230,7 @@ def extract_zarr_variable_encoding(
     """
     encoding = variable.encoding.copy()
 
+    safe_to_drop = {"source", "original_shape"}
     valid_encodings = {
         "chunks",
         "compressor",
@@ -238,6 +239,10 @@ def extract_zarr_variable_encoding(
         "write_empty_chunks",
     }
 
+    for k in safe_to_drop:
+        if k in encoding:
+            del encoding[k]
+
     if raise_on_invalid:
         invalid = [k for k in encoding if k not in valid_encodings]
         if invalid: