Repository: esmvalgroup/esmvalcore
Issue: Wrong error message for duplicate coordinate

The error message which appears in #371 is:

```
esmvalcore.cmor.check.CMORCheckError: There were errors in variable tas:
height2m: does not exist
in cube:
air_temperature / (K)               (time: 1200; latitude: 180; longitude: 288)
     Dimension coordinates:
          time                           x               -               -
          latitude                       -               x               -
          longitude                      -               -               x
     Scalar coordinates:
          height: 2.0 m
             description='~2 m standard surface air temperature and surface humidity  height'
          height: 2.0 m
     Attributes:
          Conventions: CF-1.7 CMIP-6.0 UGRID-1.0
          activity_id: CMIP
          branch_method: standard
          branch_time_in_child: -674885.0
          branch_time_in_parent: 0.0
          comment: <null ref>
          contact: gfdl.climate.model.info@noaa.gov
          data_specs_version: 01.00.27
          experiment: pre-industrial control
          experiment_id: piControl
          external_variables: areacella
          forcing_index: 1
          frequency: monC
          further_info_url: https://furtherinfo.es-doc.org/CMIP6.NOAA-GFDL.GFDL-CM4.piControl.none...
          grid: atmos data regridded from Cubed-sphere (c96) to 180,288; interpolation...
          grid_label: gr1
          initialization_index: 1
          institution: National Oceanic and Atmospheric Administration, Geophysical Fluid Dynamics...
          institution_id: NOAA-GFDL
          interp_method: conserve_order2
          license: CMIP6 model data produced by NOAA-GFDL is licensed under a Creative Commons...
          mip_era: CMIP6
          nominal_resolution: 100 km
          original_name: tas
          parent_activity_id: CMIP
          parent_experiment_id: piControl-spinup
          parent_mip_era: CMIP6
          parent_source_id: GFDL-CM4
          parent_time_units: days since 1850-1-1 00:00:00
          parent_variant_label: r1i1p1f1
          physics_index: 1
          product: model-output
          realization_index: 1
          realm: atmos
          references: see further_info_url attribute
          source: GFDL-CM4 (2018): 
aerosol: interactive
atmos: GFDL-AM4.0.1 (Cubed-sphere...
          source_file: /mnt/lustre02/work/bd0854/DATA/ESMValTool2/CMIP6_DKRZ/CMIP/NOAA-GFDL/G...
          source_id: GFDL-CM4
          source_type: AOGCM
          sub_experiment: none
          sub_experiment_id: none
          table_id: Amon
          title: NOAA GFDL GFDL-CM4 model output prepared for CMIP6 pre-industrial cont...
          variable_id: tas
          variant_info: N/A
          variant_label: r1i1p1f1
     Cell methods:
          mean: area, time
```

However, the coordinate `height` is not missing, but appears twice (because it was additionally added by the fix). This can be very confusing.

The reason for this is that `iris` also raises an `CoordinateNotFoundError` for `cube.coord('coord_name')` if `'coord_name'` appears more than once in `cube`:

```
iris.exceptions.CoordinateNotFoundError: 'Expected to find exactly 1 coordinate, but found 2. They were: ..., ....'
```


Changed files:
tests/unit/cmor/test_cmor_check.py, esmvalcore/cmor/check.py

diff --git a/esmvalcore/cmor/check.py b/esmvalcore/cmor/check.py
index c848531fc..c476019ee 100644
--- a/esmvalcore/cmor/check.py
+++ b/esmvalcore/cmor/check.py
@@ -100,6 +100,7 @@ class CMORCheck():
 
         self._check_var_metadata()
         self._check_fill_value()
+        self._check_multiple_coords_same_stdname()
         self._check_dim_names()
         self._check_coords()
         if self.frequency != 'fx':
@@ -283,6 +284,21 @@ class CMORCheck():
             self.report_error(self._does_msg, self._cube.var_name,
                               'match coordinate rank')
 
+    def _check_multiple_coords_same_stdname(self):
+        standard_names = set()
+        for coord in self._cube.coords():
+            if coord.standard_name:
+                if coord.standard_name in standard_names:
+                    coords = [c.var_name for c in self._cube.coords(
+                        standard_name=coord.standard_name)
+                    ]
+                    self.report_error(
+                        'There are multiple coordinates with '
+                        f'standard_name "{coord.standard_name}": {coords}'
+                    )
+                else:
+                    standard_names.add(coord.standard_name)
+
     def _check_dim_names(self):
         """Check dimension names."""
         for (_, coordinate) in self._cmor_var.coordinates.items():
diff --git a/tests/unit/cmor/test_cmor_check.py b/tests/unit/cmor/test_cmor_check.py
index e7c03d2d5..2c32cc83b 100644
--- a/tests/unit/cmor/test_cmor_check.py
+++ b/tests/unit/cmor/test_cmor_check.py
@@ -175,6 +175,19 @@ class TestCMORCheck(unittest.TestCase):
         self._check_cube()
         assert 'time_origin' not in self.cube.coord('time').attributes
 
+    def test_check_no_multiple_coords_same_stdname(self):
+        """Test checks fails if two coords have the same standard_name."""
+        self.cube.add_aux_coord(
+            iris.coords.AuxCoord(
+                np.reshape(np.linspace(-90, 90, num=20*20), (20, 20)),
+                var_name='bad_name',
+                standard_name='latitude',
+                units='degrees_north'
+            ),
+            (1, 2)
+        )
+        self._check_fails_in_metadata()
+
     def test_check_bad_standard_name_auto_fix(self):
         """Test check pass for a bad standard_name with automatic fixes."""
         self.cube = self.get_cube(self.var_info)