@@ -174,11 +174,11 @@ def _get_axis_coord_single(var: Union[DataArray, Dataset], key: str,) -> List[st
174174 """ Helper method for when we really want only one result per key. """
175175 results = _get_axis_coord (var , key )
176176 if len (results ) > 1 :
177- raise ValueError (
177+ raise KeyError (
178178 f"Multiple results for { key !r} found: { results !r} . Is this valid CF? Please open an issue."
179179 )
180180 elif len (results ) == 0 :
181- raise ValueError (f"No results found for { key !r} ." )
181+ raise KeyError (f"No results found for { key !r} ." )
182182 return results
183183
184184
@@ -895,14 +895,17 @@ def __getitem__(self, key: Union[str, List[str]]):
895895 )
896896
897897 if scalar_key :
898+ axis_coord_mapper = _get_axis_coord_single
898899 key = (key ,) # type: ignore
900+ else :
901+ axis_coord_mapper = _get_axis_coord
899902
900903 varnames : List [Hashable ] = []
901904 coords : List [Hashable ] = []
902905 successful = dict .fromkeys (key , False )
903906 for k in key :
904907 if k in _AXIS_NAMES + _COORD_NAMES :
905- names = _get_axis_coord (self ._obj , k )
908+ names = axis_coord_mapper (self ._obj , k )
906909 successful [k ] = bool (names )
907910 coords .extend (names )
908911 elif k in _CELL_MEASURES :
@@ -919,6 +922,7 @@ def __getitem__(self, key: Union[str, List[str]]):
919922 # these are not special names but could be variable names in underlying object
920923 # we allow this so that we can return variables with appropriate CF auxiliary variables
921924 varnames .extend ([k for k , v in successful .items () if not v ])
925+ allnames = varnames + coords
922926
923927 try :
924928 for name in varnames :
@@ -929,8 +933,10 @@ def __getitem__(self, key: Union[str, List[str]]):
929933 else :
930934 ds = self ._obj
931935
932- if scalar_key and len (varnames ) == 1 :
933- da : DataArray = ds [varnames [0 ]].reset_coords (drop = True ) # type: ignore
936+ if scalar_key and len (allnames ) == 1 :
937+ da : DataArray = ds .reset_coords ()[allnames [0 ]] # type: ignore
938+ if allnames [0 ] in coords :
939+ coords .remove (allnames [0 ])
934940 failed = []
935941 for k1 in coords :
936942 if k1 not in ds .variables :
0 commit comments